package com.itheima.mobilesafe74.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.itheima.mobilesafe74.R;
import com.itheima.mobilesafe74.engine.SmsBackUp;

import java.io.File;


public class AToolActivity extends Activity {
    private TextView tv_query_phone_address;
    private TextView tv_sms_backup;
    private ProgressBar pd_bar;
    private TextView tv_commonnumber_query;
    private TextView tv_app_lock;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_atool);

        //电话归属地查询方法
        initPhoneAddress();
        //短信备份的方法
        initSmsBackUp();
        //常用号码查询的方法
        initCommonNumberQuery();
        initAppLock();

    }

    private void initAppLock() {
        tv_app_lock=(TextView)findViewById(R.id.tv_app_lock);
        tv_app_lock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),AppLockActivity.class));
            }
        });
    }

    private void initCommonNumberQuery() {
        tv_commonnumber_query=(TextView)findViewById(R.id.tv_commonnumber_query);
        tv_commonnumber_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),CommonNumberQueryActivity.class));
            }
        });
    }

    private void initSmsBackUp() {
        tv_sms_backup= (TextView) findViewById(R.id.tv_sms_backup);
        pd_bar= (ProgressBar) findViewById(R.id.pd_bar);
        tv_sms_backup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSmsBackUpDialog();
            }
        });
    }

    //
    //
    private void showSmsBackUpDialog() {
        //1、创建一个带进度条的对话框
        final ProgressDialog progressDialog=new ProgressDialog(this);
        progressDialog.setIcon(R.mipmap.ic_launcher);
        progressDialog.setTitle("短信备份");
        //2、指定进度条的样式为水平
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //3、展示进度条
        progressDialog.show();
        //4、直接调用备份短信的方法
        new Thread(){
            @Override
            public void run() {
                //获取sd卡的路径
                String path=Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator+"sms74";
                //SmsBackUp.backup(getApplicationContext(),path,progressDialog);
                SmsBackUp.backup(getApplicationContext(), path, new SmsBackUp.CallBack() {
                    @Override
                    public void setMax(int max) {
                        //由开发者自己决定，使用对话框还是进度条
                        progressDialog.setMax(max);
                        pd_bar.setMax(max);
                    }

                    @Override
                    public void setProgress(int index) {
                        progressDialog.setProgress(index);
                        pd_bar.setProgress(index);
                    }
                });
                progressDialog.dismiss();
            }
        }.start();

    }

    private void initPhoneAddress() {
        tv_query_phone_address= (TextView) findViewById(R.id.tv_query_phone_address);
        tv_query_phone_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),QueryAddressActivity.class));
            }
        });
    }
}
